package com.ruoyi.common.utils.stock;

import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;

import java.util.Map;

/**
 * 个股工具类
 */
public class StockUtils {

    /**
     * 设置字典缓存
     *
     * @param key 参数键
     * @param shareDate 字典数据
     */
    public static void setShareCache(String key, Map<Object,String> shareDate)
    {
        SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(key), shareDate);
    }

    /**
     * 获取字典缓存
     *
     * @param key 参数键
     * @return
     */
    public static Boolean getShareCache(String key,String shareCode,String shareName)
    {
        boolean result = true;
        Map<Object,String> mapCache = SpringUtils.getBean(RedisCache.class).getCacheObject(getCacheKey(key));
        if (StringUtils.isNotNull(mapCache))
        {
            if (StringUtils.isEmpty(mapCache.get(shareCode))){
                result=false;
                mapCache.put(shareCode,shareName);
                SpringUtils.getBean(RedisCache.class).setCacheObject(getCacheKey(key), mapCache);
            }
        }
        return result;
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(String configKey)
    {
        return CacheConstants.STOCK_KEY + configKey;
    }
}
